import {
    Component,
    ViewChild,
    Injector,
    Output,
    EventEmitter,
    ElementRef,
} from '@angular/core';
import { ModalDirective } from 'ngx-bootstrap';
import {
    ComboboxItemDto,
    OrganizationUnitServiceProxy,
    NameValueDto,
    GetUserWorkingDto,
    UserWorkingTimeServiceProxy,
    UserWorkingTimeInput,
    CreateDetailWorkingInput,
    GetProcWorkingTimeDto,
} from '@shared/service-proxies/service-proxies';
import { AppComponentBase } from '@shared/common/app-component-base';

import * as moment from 'moment';
import { finalize } from 'rxjs/operators';
import { LazyLoadEvent } from 'primeng/api';

@Component({
    selector: 'createOrEditTaskworkingModal',
    styleUrls: ['./create-or-edit-taskworking-modal.component.less'],
    templateUrl: './create-or-edit-taskworking-modal.component.html',
})
export class CreateOrEditTaskworkingModalComponent extends AppComponentBase {
    @ViewChild('createOrEditModal') modal: ModalDirective;
    @ViewChild('dueDatePicker') dueDatePicker: ElementRef;

    @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();
    id: number;
    active = false;
    saving = false;
    nodeWorker = new Array<GetUserWorkingDto>();
    proc: GetProcWorkingTimeDto;
    displayDialog: boolean;
    users = new Array<NameValueDto>();
    totalworkingtime = 0;
    userworkingtime = 0;
    total=0;
    userworkingtimestring: string;
    event: LazyLoadEvent;
    constructor(
        injector: Injector,
        private _organizationUnitServiceProxy: OrganizationUnitServiceProxy,
        private _userWorkingTimeServiceProxy: UserWorkingTimeServiceProxy
    ) {
        super(injector);
    }

    show(proc: GetProcWorkingTimeDto, event?: LazyLoadEvent): void {
        this.primengTableHelper.showLoadingIndicator();
        this.proc = proc;
        this.event = event;
        this.users = null;
        this.userworkingtime = 0;
        this.total=0;
        this._userWorkingTimeServiceProxy
            .getProcWorkersById(proc.id)
            .subscribe((result) => {
                this.nodeWorker = result;
                this._organizationUnitServiceProxy
                    .findUsersForNode()
                    .subscribe((result) => {
                        this.users = result;
                        this.primengTableHelper.hideLoadingIndicator();
                    });
                this.nodeWorker.forEach((item) => {
                    this.userworkingtime =
                        this.userworkingtime + item.personWorkingTime;
                });
                //this.userworkingtimestring = this.userworkingtime.toFixed(2);
                this._userWorkingTimeServiceProxy
                .getTotalPersonWorkingTime(proc.id)
                .subscribe((result) => {
                    console.log(result);
                    this.total = result-this.userworkingtime;
                    this.active = true;
                    this.modal.show();
                });
            });
           

       
    }
    jisuan() {
        this.userworkingtime = 0;
        this.nodeWorker.forEach((item) => {
            this.userworkingtime =
                this.userworkingtime + item.personWorkingTime;
        });
       // this.userworkingtimestring = this.userworkingtime.toFixed(2);
    }
    showDialogToAdd() {
        let selected = new GetUserWorkingDto();
        selected.userName = new NameValueDto();
        this.userworkingtime=0;
        selected.userName = this.users[0];
        selected.personWorkingTime = 0;
        this.nodeWorker.push(selected);
        console.log(this.nodeWorker);
        this.nodeWorker.forEach((item) => {
            this.userworkingtime =
                this.userworkingtime + item.personWorkingTime;
        });
       // this.userworkingtimestring = this.userworkingtime.toFixed(2);
    }
    save(): void {
        if (
            this.proc.totalProcWorkingTime  <
            this.userworkingtime+ this.total
        ) {
            this.notify.error('分配工时不能大于总工时');
        } else {
            this.saving = true;
            let nodeWorkers = new CreateDetailWorkingInput();
            nodeWorkers.workingDtos = this.nodeWorker;
            nodeWorkers.procId = this.proc.id;
            this._userWorkingTimeServiceProxy
                .createOrUpdateDetailByProc(nodeWorkers)
                .subscribe((result) => {
                    this.notify.success("保存成功");
                    this.close();
                    this.modalSave.emit(this.event);
                });
        }
    }
    close(): void {
        this.active = false;
        this.modal.hide();
    }
    onRowEditInit(data) {
        console.log(data);
        console.log(data);
        let nodeWorkers = new Array<GetUserWorkingDto>();
        for (let index = 0; index < this.nodeWorker.length; index++) {
            if (index !== data) {
                nodeWorkers.push(this.nodeWorker[index]);
            }
        }
        this.nodeWorker = nodeWorkers;
    }
}
